import java.util.ArrayList;
import java.util.List;

public class SolutionInterview8_9 {
    public List<String> generateParenthesis(int n) {
        List<String> list=new ArrayList<>();
        dfs(list,n,0,0,new StringBuilder());
        return list;
    }

    private void dfs(List<String> list, int n,int t, int k, StringBuilder stringBuilder) {
        if (stringBuilder.length()==n*2){
            list.add(stringBuilder.toString());
            return;
        }
        if (k>0){
            stringBuilder.append(')');
            dfs(list,n,t,k-1,stringBuilder);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
        if (t<n){
            stringBuilder.append('(');
            dfs(list,n,t+1,k+1,stringBuilder);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
    }

    public static void main(String[] args) {
        System.out.println(new SolutionInterview8_9().generateParenthesis(3));
    }
}
